package com.staryy.activiti7_workflow;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;
import java.util.Map;

/**
 * UEL表达式
 */
@SpringBootTest
public class Part6_UEL {

    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private TaskService taskService;


    /**
     * 发布流程
     */
    @Test
    public void initDeployment(){
        repositoryService.createDeployment()
                .addClasspathResource("BPMN/UEL_V3.bpmn")
                .name("实体类参数demo")
                .deploy();
    }

    // 启动流程实例带参数，指定执行人
    @Test
    public void startProcessInstanceWithArgs(){
        Map<String,Object> varMap = new HashMap<>();
        varMap.put("ZhiXingRen","zhangsan");
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("myProcess_UEL_V1", "myProcess_UEL_V1_001", varMap);
        System.out.println(processInstance.getProcessInstanceId());
    }
    // 完成任务带参数,根据参数执行下一操作人
    @Test
    public void complateTaskWithArgs(){
        Map<String,Object> varMap = new HashMap<>();
        varMap.put("pay","1000");
        taskService.complete("09642ae7-0b42-11ed-8cd4-9cda3ebfe416",varMap);
    }

    // 启动流程实例带参数，使用实体类
    @Test
    public void startProcessInstanceWithClassArgs(){
        UelPojo pojo = new UelPojo();
        pojo.setCode("liudehua");
        Map<String,Object> varMap = new HashMap<>();
        varMap.put("uelpojo",pojo);
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("myProcess_UEL_V3", "myProcess_UEL_V3_001", varMap);
        System.out.println(processInstance.getProcessInstanceId());

    }
    // 任务完成环节，指定多个候选人
    @Test
    public void completeTaskToManyUsers(){
        Map<String,Object> varMap = new HashMap<>();
        varMap.put("houxuanren","zhangsan,lisi");
        taskService.complete("411e5841-0b45-11ed-9e02-9cda3ebfe416",varMap);
    }
}
